/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface;

import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerManager;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PanelWorkspace extends JPanel implements Observer, MouseMotionListener, MouseListener
{
    private static final int M_TOP_LEFT_POINT_X = 0;
    private static final int M_TOP_LEFT_POINT_Y = 0;
    
    private LayerManager     m_layerManager;
    private Frame            m_application;
    private Color            m_colourToDraw;
    private Image            m_canvas;
    private int              m_oldX             = 0;
    private int              m_oldY             = 0;
    private int              m_x                = 0;
    private int              m_y                = 0;

    public PanelWorkspace (LayerManager layerManager, JFrame panel)
    {
        this.m_application  = panel;
        this.m_layerManager = layerManager;
        
        this.m_layerManager.addObserver(this);
    }

    public void drawOn ()
    {
        addMouseListener      (this);
        addMouseMotionListener(this);
    }

    public void setColorToDraw (Color c)
    {
        m_colourToDraw = c;
    }

    public void drawOff ()
    {
        removeMouseListener      (this);
        removeMouseMotionListener(this);
    }

    @Override
    protected void paintComponent (Graphics g)
    {
        super.paintComponent(g);
        if (m_canvas == null)
        {
            m_canvas = new BufferedImage(this.m_layerManager.getM_widthWithBorder(), this.m_layerManager.getM_heightWithBorder(), ALayer.M_DEFAULT_IMAGE_TYPE);

            Graphics2D canvasG = (Graphics2D) m_canvas.getGraphics();
            canvasG.dispose();
        }
        else
        {
            Graphics2D canvasG = (Graphics2D) m_canvas.getGraphics();
            canvasG.setColor(m_colourToDraw);
            canvasG.drawLine(m_oldX, m_oldY, m_x, m_y);
            canvasG.dispose();
        }

        m_oldX = m_x;
        m_oldY = m_y;

        this.setPreferredSize(new Dimension(this.m_layerManager.getM_widthWithBorder(), this.m_layerManager.getM_heightWithBorder()));
        
        this.m_layerManager.drawMeWithDelimitedBorder((Graphics2D) g);
        
        g.drawImage(m_canvas, M_TOP_LEFT_POINT_X, M_TOP_LEFT_POINT_Y, this);
    }

    @Override
    public void mouseDragged (MouseEvent e)
    {
        m_x = e.getX();
        m_y = e.getY();
        
        repaint();
    }

    @Override
    public void update (Observable o, Object arg)
    {
        this.repaint();
    }

    public void setM_colourToDraw (Color m_colourToDraw)
    {
        this.m_colourToDraw = m_colourToDraw;
    }

    @Override
    public void mouseMoved (MouseEvent e)
    {
    }

    @Override
    public void mouseClicked (MouseEvent e)
    {
    }

    @Override
    public void mousePressed (MouseEvent e)
    {
        m_oldX = m_x = e.getX();
        m_oldY = m_y = e.getY();
    }

    @Override
    public void mouseReleased (MouseEvent e)
    {
    }

    @Override
    public void mouseEntered (MouseEvent e)
    {
    }

    @Override
    public void mouseExited (MouseEvent e)
    {
    }
}
